﻿using System;
using System.Collections.Generic;

using System.Web;
using DotNETX.Authentication;
using DotNETX.Web.Authority;

namespace DotNETX.Web.Authority
{
    public abstract class BaseUserAuthorityValidater : IAuthorityValidater
    {
        public static IAuthenticater Authenticater 
        { 
            get
            {
                return DotNETX.Web.Handlers.LoginHandler.Authenticater;
            }
        }

        public static string GetToken(HttpContext context)
        {
            return DotNETX.Web.Handlers.LoginHandler.GetToken(context);
        }

        public virtual AuthorityValidateResult Validate(HttpContext context)
        {

            if(Authenticater != null)
            {
                string token = GetToken(context);
                string userid = Authenticater.GetUId(token);
                if (userid == null)
                {
                    return new AuthorityValidateResult() { Level = 1, Passable = false };
                }
                else
                {
                    return UserAuthorityValidate(context, userid);
                }
            }
            else
            {
                return new AuthorityValidateResult() { Level = 1, Passable = false };
            }

        }

        public abstract AuthorityValidateResult UserAuthorityValidate(HttpContext context, string userid);
    }
}
